#include "gobangwidget.h"

GobangWidget::GobangWidget(QWidget* parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_StyledBackground, true);
    setObjectName("goBangWidget");
    initUI();
    setupStyles();
}

GobangWidget::~GobangWidget()
{
}


void GobangWidget::initUI()
{
    

    // 创建主布局
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setSpacing(10);
    m_mainLayout->setContentsMargins(20, 20, 20, 20);

    // 创建控制面板
    m_controlLayout = new QHBoxLayout();

    // 创建按钮
    m_restartBtn = new QPushButton("重新开始");
    m_regretBtn = new QPushButton("悔棋");
    m_aiBtn = new QPushButton("AI对战");
    m_statusLabel = new QLabel("当前回合: 黑棋 ●");
    m_timerLabel = new QLabel("游戏时间: 00:00");

    // 添加到控制布局
    m_controlLayout->addWidget(m_restartBtn);
    m_controlLayout->addWidget(m_regretBtn);
    m_controlLayout->addWidget(m_aiBtn);
    m_controlLayout->addStretch();
    m_controlLayout->addWidget(m_statusLabel);
    m_controlLayout->addWidget(m_timerLabel);

    // 创建棋盘控件
    m_chessBoard = new ChessBoardWidget(this);

    // 添加到主布局
    m_mainLayout->addLayout(m_controlLayout,1);
    m_mainLayout->addWidget(m_chessBoard,10);


    // 连接信号槽
    connect(m_restartBtn, &QPushButton::clicked, this, &GobangWidget::onRestartClicked);
    connect(m_regretBtn, &QPushButton::clicked, this, &GobangWidget::onRegretClicked);
    connect(m_aiBtn, &QPushButton::clicked, this, &GobangWidget::onAIClicked);

    // 连接棋盘信号
    connect(m_chessBoard, &ChessBoardWidget::gameStateChanged, this, &GobangWidget::onGameStateChanged);
    connect(m_chessBoard, &ChessBoardWidget::gameOver, this, &GobangWidget::onGameOver);
    connect(m_chessBoard, &ChessBoardWidget::gameTimeUpdated, this, &GobangWidget::onGameTimeUpdated);
}

void GobangWidget::setupStyles()
{
    m_statusLabel->setObjectName("statusLabel");
    m_timerLabel->setObjectName("timerLabel");
    m_restartBtn->setObjectName("restartBtn");
    m_regretBtn->setObjectName("regretBtn");
    m_aiBtn->setObjectName("aiBtn");   
}

void GobangWidget::onRestartClicked()
{
    m_chessBoard->restartGame();
}

void GobangWidget::onRegretClicked()
{
    if (m_chessBoard->regretMove()) {
        // 悔棋成功，状态会在gameStateChanged信号中更新
    }
    else {
        QMessageBox::information(this, "提示", "无法悔棋！");
    }
}

void GobangWidget::onAIClicked()
{
    QMessageBox::information(this, "AI对战", "AI功能开发中...");
}

void GobangWidget::onGameStateChanged(bool isBlackTurn)
{
    m_statusLabel->setText(QString("当前回合: %1 %2")
        .arg(isBlackTurn ? "黑棋" : "白棋")
        .arg(isBlackTurn ? "●" : "○"));
}

void GobangWidget::onGameOver(int winner)
{
    QString winnerText = (winner == 1) ? "黑棋" : "白棋";
    QMessageBox::information(this, "游戏结束",
        QString("%1胜利！").arg(winnerText));
}

void GobangWidget::onGameTimeUpdated(int seconds)
{
    int minutes = seconds / 60;
    int secs = seconds % 60;
    m_timerLabel->setText(QString("游戏时间: %1:%2")
        .arg(minutes, 2, 10, QLatin1Char('0'))
        .arg(secs, 2, 10, QLatin1Char('0')));
}